package cn.netty.exemple7;

import cn.netty.exemple7.protobuf.SubscribeRepsProto;
import cn.netty.exemple7.protobuf.SubscribeReqProto;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ProtoClientHandel extends ChannelHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            SubscribeRepsProto.SubscribeReps msg1 = (SubscribeRepsProto.SubscribeReps) msg;
            System.out.println(msg1.toString());
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        for (int i = 0; i < 10; i++) {
            SubscribeReqProto.SubscribeReq send = send(i);
            ctx.writeAndFlush(send);
        }
        //ctx.flush();
    }

    private SubscribeReqProto.SubscribeReq send(int i){
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();

        builder.setAddress("广州天河");
        builder.setUserName("陈志鹏");
        builder.setPreductName("just netty");
        builder.setId(i);
        return builder.build();
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("client is error:"+cause.getMessage());
        ctx.close();
    }
}
